﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Pur
{
    public partial class frmMaintainSupplierQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstSupplierEntity mSelectedSupplier;
        public MstSupplierEntity SelectedSupplier
        {
            get { return mSelectedSupplier; }
        }
        #endregion

        #region Constructor
        public frmMaintainSupplierQuery()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtSupplierCode.Text) == false)
            {
                condition += " AND SUPPLIER_CODE LIKE '%" + txtSupplierCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtSupplierName.Text) == false)
            {
                condition += " AND SUPPLIER_NAME LIKE '%" + txtSupplierName.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                condition += " AND STATUS='" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "SUPPLIER_CODE";
            DataTable dt = MstSupplierBs.GetMstSupplierByCondition("", condition, orderBy);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindCombobox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedSupplier = new MstSupplierEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedSupplier == null)
            {
                MessageBox.Show("请选择一个供应商！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }
        #endregion
    }
}
